import { Router, NavigationStart } from '@angular/router';
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class PageTitleService {
  private op;
  constructor(
    private router: Router
  ) {
    this.router.events.subscribe((event) => {
      // console.log(event);
      if (event instanceof NavigationStart) { // 当导航成功结束时执行
        this.op = null;
        this.OnPageTitleChange && this.OnPageTitleChange(null);
      }
    })
  }

  setPageHeaderOption(op: PageHeaderOption) {
    this.op = op;
    this.OnPageTitleChange && this.OnPageTitleChange(this.op)
  }

  OnPageTitleChange: (op: PageHeaderOption) => void;
}

export interface PageHeaderOption {
  title: string;
  subTitle?: string;
  buttons?: PageHeaderOptionButton[];
}

interface PageHeaderOptionButton {
  icon?: string;
  text: string;
  ngType?: string;
  buttonType?: 'button' | 'upload' | 'groupButton';
  GroupName?: string;
  children?: PageHeaderOptionButton[];
  onClick?: () => void;
  beforeUpload?: (file) => void;
}
